import {Effect} from 'dva';
import {Reducer} from 'redux';

import {query as queryUsers} from '@/services/user';
import {history} from "@@/core/history";

export interface CurrentUser {
  avatar?: string;
  name?: string;
  title?: string;
  group?: string;
  signature?: string;
  tags?: {
    key: string;
    label: string;
  }[];
  userid?: string;
  unreadCount?: number;
}

export interface UserModelState {
  currentUser?: CurrentUser;
}

export interface UserModelType {
  namespace: 'user';
  state: UserModelState;
  effects: {
    fetch: Effect;
    fetchCurrent: Effect;
  };
  reducers: {
    saveCurrentUser: Reducer<UserModelState>;
    changeNotifyCount: Reducer<UserModelState>;
  };
}

const UserModel: UserModelType = {
  namespace: 'user',

  state: {
    currentUser:
      {
        name: '匿名',
        avatar: 'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png',
      },
  },

  effects: {
    * fetch(_, {call, put}) {
      const response = yield call(queryUsers);
      yield put({
        type: 'save',
        payload: response,
      });
    },
    * fetchCurrent(_, {call, put}) {
      // if (isProductionEnv) return;
      if (!localStorage.getItem("userName")) {
        // Note: There may be security issues, please note
        localStorage.setItem('token', "");
        localStorage.setItem('userName', "");
        localStorage.setItem('userTypes', "");
        history.replace({
          pathname: '/user/login',
        });
      }
      const response = {
        name: localStorage.getItem("userName"),
        userid: '00000001',
        avatar: 'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png',
        // email: 'antdesign@alipay.com',
        // signature: '海纳百川，有容乃大',
        // title: '交互专家',
        // group: '蚂蚁金服－某某某事业群－某某平台部－某某技术部－UED',
        // tags: [
        //   {
        //     key: '0',
        //     label: '很有想法的',
        //   },
        //   {
        //     key: '1',
        //     label: '专注设计',
        //   },
        //   {
        //     key: '2',
        //     label: '辣~',
        //   },
        //   {
        //     key: '3',
        //     label: '大长腿',
        //   },
        //   {
        //     key: '4',
        //     label: '川妹子',
        //   },
        //   {
        //     key: '5',
        //     label: '海纳百川',
        //   },
        // ],
        // notifyCount: 12,
        // unreadCount: 11,
        // country: 'China',
        // geographic: {
        //   province: {
        //     label: '浙江省',
        //     key: '330000',
        //   },
        //   city: {
        //     label: '杭州市',
        //     key: '330100',
        //   },
        // },
        // address: '西湖区工专路 77 号',
        // phone: '0752-268888888',
      };
      yield put({
        type: 'saveCurrentUser',
        payload: response,
      });
    },
  },

  reducers: {
    saveCurrentUser(state, action) {
      return {
        ...state,
        currentUser: action.payload || {},
      };
    },
    changeNotifyCount(
      state = {
        currentUser: {},
      },
      action,
    ) {
      return {
        ...state,
        currentUser: {
          ...state.currentUser,
          notifyCount: action.payload.totalCount,
          unreadCount: action.payload.unreadCount,
        },
      };
    },
  },
};

export default UserModel;
